package com.design.softwaredesign.service.impl;

import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.entity.IwantTrace;
import com.design.softwaredesign.front.IwantInfo;
import com.design.softwaredesign.mapper.IwantMapper;
import com.design.softwaredesign.service.IwantService;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.GoodVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/1 10:17
 */
@Service
public class IwantServiceImpl implements IwantService {
    @Autowired
    private IwantMapper iwantMapper;

    @Override
    public DataVo addIwant(IwantInfo iwantInfo) {
        DataVo dataVo = new DataVo();
        IwantTrace iwantTrace = new IwantTrace();
        try {
            BeanUtils.copyProperties(iwantInfo, iwantTrace);
            iwantTrace.setIwantTraceId(UUID.randomUUID().toString());
            iwantTrace.setUserId_buyer(iwantInfo.getUserId());
            iwantMapper.addIwant(iwantTrace);
            dataVo.setCode(200);
            dataVo.setMsg("添加我想要成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("添加我想要失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public DataVo deleteIwantTraceById(IwantInfo iwantInfo) {
        DataVo dataVo = new DataVo();
        IwantTrace iwantTrace = new IwantTrace();
        dataVo.setCode(202);
        dataVo.setMsg("删除预购记录失败");
        try {
            iwantTrace.setUserId_buyer(iwantInfo.getUserId());
            iwantTrace.setGoodId(iwantInfo.getGoodId());
            if(iwantMapper.deleteIwantTrace(iwantTrace) == 1) {
                dataVo.setCode(200);
                dataVo.setMsg("删除预购记录成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public DataVo getIwantList(String userId, Integer pageNum, Integer pageSize, String query) {
        DataVo dataVo = new DataVo();
        List<Good> goodslist = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            goodslist = iwantMapper.getIwantList(userId,query);
            PageInfo<Good> goodPageInfo = new PageInfo<>(goodslist);
            List<GoodVo> goodVoList = new ArrayList<>();
            Good good = null;
            for(int i = 0; i < goodslist.size(); i++) {
                GoodVo goodVo = new GoodVo();
                good = goodslist.get(i);
                BeanUtils.copyProperties(good, goodVo);
                if(!good.getImageUrl().equals("")) {
                    String []imageUrls = good.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    goodVo.setImageUrlList(imageUrlList);
                }
                goodVoList.add(goodVo);
            }
            dataVo.setCode(200);
            dataVo.setCount(goodPageInfo.getTotal());
            dataVo.setMsg("获取欲购记录成功");
            dataVo.setData(goodVoList);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("获取欲购记录失败");
            e.printStackTrace();
        } finally {
            return  dataVo;
        }
    }
}
